package com.sanmu.Test;

import com.sanmu.bean.BookStore;
import com.sanmu.bean.Emp;
import com.sanmu.bean.Master;
import com.sanmu.bean.Monster;
import com.sanmu.component.UserAction;
import com.sanmu.component.UserDao;
import com.sanmu.component.UserService;
import com.sanmu.dao.OrderDao;
import com.sanmu.sanmuapplicationContext.SmuApplicationContext;
import com.sanmu.service.MemberServiceImpl;
import com.sanmu.service.OrderService;
import com.sanmu.web.OrderController;
import org.dom4j.DocumentException;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @version 1.0
 * @Author xinnian
 * @Date 2024/9/13 11:09
 * @注释
 * @create: 2024-09-13 11:09
 */

public class SpringBeanTest {
    public void getBeanByType() throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        ApplicationContext ioc = (ApplicationContext) new SmuApplicationContext("beans.xml");
        //按照类型获取bean 容器中只有一个bean对象

    }
    /**
     *
     * 通过构造器来设置属性
     * */
    public void setBeanByConstructor() throws DocumentException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        ApplicationContext ioc = (ApplicationContext) new SmuApplicationContext("beans.xml");

    }


    /**
     * 通过ref 来注入bean 对象
     * */
    @Test
    public void setBeanByRef()  {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
        MemberServiceImpl memberService = ioc.getBean("memberService", MemberServiceImpl.class);
        System.out.println(ioc.getBean("memberService", memberService));

    }
    /**
     * 获取List
     * */
    @Test
    public void getBeanList(){
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
/*        Master master = ioc.getBean("master", Master.class);

        System.out.println(master.getMonsterList());
        System.out.println(master.getMonsterSet());
        System.out.println("monsterName"+master.getMonsterName()[0]);
        Master master02 = ioc.getBean("master2", Master.class);
        System.out.println(master02.getMonsterMap());
        BookStore bookStore = ioc.getBean("bookStore", BookStore.class);
        System.out.println("书名："+bookStore.getBookList());
        Emp emp = ioc.getBean("emp", Emp.class);
        System.out.println("员工"+ emp.getName() + " 部门" + emp.getDept().getName());


        Monster monster09 = ioc.getBean("monster09", Monster.class);
        System.out.println(monster09);

        Monster monster10 = ioc.getBean("monster10", Monster.class);
        System.out.println(monster10);

        Monster monster11 = ioc.getBean("myFactoryBean", Monster.class);
        System.out.println(monster11);*/
/*
        Monster monster1000 = ioc.getBean("monster1000", Monster.class);
        System.out.println(monster1000.getMonsterId() + " " + monster1000.getName() + " " + monster1000.getsKill());*/



    }
    @Test
    public void setBeanByaotowing() {
            ApplicationContext ioc = new ClassPathXmlApplicationContext("bean03.xml");
        OrderController orderController = ioc.getBean("orderController", OrderController.class);
        System.out.println("获取到的Service"+ orderController.getOrderService());
        OrderService orderService = ioc.getBean("orderService", OrderService.class);
        System.out.println("获取到的Dao"+ orderService.getOrderDao());
        OrderDao orderDao = ioc.getBean("orderDao", OrderDao.class);
        System.out.println("获取到的Dao"+ orderDao);



    }
    @Test
    public void TestByAutoUpdate() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("AutoSetBeans.xml");
      UserService Service = ioc.getBean("userService", UserService.class);
        UserDao userDao = ioc.getBean("userDao", UserDao.class);
        UserAction userAction = ioc.getBean("userAction", UserAction.class);
        System.out.println(userAction);

        System.out.println(userDao);

        System.out.println(Service);
        System.out.println("ok");


    }

}
